precision mediump float;

#define MAX_BOXES 50  // 最大支持 50 个框，可根据需求调整

uniform sampler2D uSampler;
uniform int uBoxCount;

uniform vec2 uCenters[MAX_BOXES]; // 每个框的中心
uniform vec2 uSizes[MAX_BOXES];   // 每个框的宽高
uniform float uBorders[MAX_BOXES];// 每个框的边框厚度
uniform vec4 uColors[MAX_BOXES];  // 每个框的颜色

varying vec2 vTextureCoord;

void main() {
  vec4 baseColor = texture2D(uSampler, vTextureCoord);
  vec4 finalColor = baseColor;

  for (int i = 0; i < MAX_BOXES; i++) {
    if (i >= uBoxCount) break;

    vec2 halfSize = uSizes[i] * 0.5;
    vec2 minPos = uCenters[i] - halfSize;
    vec2 maxPos = uCenters[i] + halfSize;

    bool insideX = (vTextureCoord.x >= minPos.x && vTextureCoord.x <= maxPos.x);
    bool insideY = (vTextureCoord.y >= minPos.y && vTextureCoord.y <= maxPos.y);

    bool borderX = (abs(vTextureCoord.x - minPos.x) < uBorders[i] ||
                    abs(vTextureCoord.x - maxPos.x) < uBorders[i]);
    bool borderY = (abs(vTextureCoord.y - minPos.y) < uBorders[i] ||
                    abs(vTextureCoord.y - maxPos.y) < uBorders[i]);

    if ((insideX && borderY) || (insideY && borderX)) {
      finalColor = uColors[i]; // 边框覆盖
    }
  }

  gl_FragColor = finalColor;
}
